<!DOCTYPE html>
<html lang="es">
<head>
  <meta charset="UTF-8">
  <title>Test de Álgebra</title>
  <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
  <script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>
  <script id="MathJax-script" async
    src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
  <style>
    body {
      font-family: Times New Roman, serif;
      margin: 40px;
      background: #f4f4f4;
    }
    .question-container {
      color: #0077cc;
      background: #fff;
      padding: 20px;
      border-radius: 8px;
      margin-bottom: 20px;
      transition: background-color 0.3s;
    }
    .options {
      margin-top: 10px;
    }
    .options label {
      display: block;
      margin-bottom: 5px;
    }
    .correct {
      background-color: #d4edda !important;
      color: #155724;
      font-weight: bold;
    }
    .incorrect {
      background-color: #f8d7da !important;
      color: #721c24;
      font-weight: bold;
    }
    #result {
      margin-top: 30px;
    }
  </style>
</head>
<body>

<h2>TEST DE ÁLGEBRA. Selecciona la respuesta correcta en cada caso: </h2>

<form id="quizForm"></form>

<div style="margin-top: 20px;">
  <button type="button" onclick="calcularResultado()">Enviar respuestas</button>
  <button type="button" onclick="reiniciarFormulario()">Limpiar y reintentar</button>
</div>

<div id="result">
  <canvas id="resultChart" width="400" height="400"></canvas>
  <div id="feedback"></div>
</div>

 
<script>
const preguntas = [
{
  texto: "La aplicación lineal \\( \\displaystyle~f~:~\\mathbb{R}^4 \\longrightarrow \\mathbb{R}^4~~\\) tal que \\( \\displaystyle~f(x,y,z,t)=(3x+y+z-t,2x-z+t,2x-t,x+y+z)~~\\) para todo \\( \\displaystyle~(x,y,z,t) \\in \\mathbb{R}^4~~\\) cumple:",
  opciones: [
    { texto: "\\( \\displaystyle~dim(Ker(f))=3\\).", correcta: false },
    { texto: "\\( \\displaystyle~dim(Ker(f))=0\\).", correcta: false },    
    { texto: "\\( \\displaystyle~dim(Ker(f))=1\\).", correcta: true },
    { texto: "Ninguna de las otras \\( 3 \\) respustass es correcta. ", correcta: false }
  ]
},
{
  texto: "La ecuación matricial  \\( \\displaystyle~\\left[ \\begin{array}{cc} -12&6\\\\ 2&-3 \\end{array}\\right]~X = \\left[ \\begin{array}{cc} -42&36\\\\ 5&-2 \\end{array}\\right]~\\) (donde \\( \\displaystyle~X~\\) es una matriz \\( \\displaystyle~2\\times 2\\)):",
  opciones: [
    { texto: "Tiene por única solución   \\( \\displaystyle~X=\\left[ \\begin{array}{cc} 4&-4\\\\ 1&-2 \\end{array}\\right]\\).", correcta: true },
    { texto: "\\( \\displaystyle~X=\\left[ \\begin{array}{cc} 4&4\\\\ -1&-2 \\end{array}\\right]~\\) es una solución, pero no la única.", correcta: false },
    { texto: "No tiene ninguna solución.", correcta: false },
    { texto: "Ninguna de las otras \\( 3 \\) respustass es correcta.  ", correcta: false }
  ]
},
{
  texto: "La aplicación lineal \\( \\displaystyle~\\begin{array}[t]{lccl}f~:& \\mathbb{R}^3 & \\longrightarrow & \\mathbb{R}^2\\\\ &(x,y,z)& \\longmapsto &f(x,y,z)=(-3x+y+2z,x+2y+5z)\\end{array}~\\):",
  opciones: [
    { texto: "Es inyectiva pero no exhaustiva.", correcta: false},
    { texto: "Es exhaustiva pero no inyectiva.", correcta: true },
    { texto: "Es biyectiva.", correcta: false},
    { texto: "No es ni inyectiva ni exhaustiva.", correcta: false}
  ]
},
{
  texto: "Sean \\( \\displaystyle~A,~B~\\) dos matrices de tipo \\( \\displaystyle~m\\times n~\\) tales que \\( \\displaystyle~A {\\buildrel {e_1}\\over \\longrightarrow}~{\\buildrel {e_2}\\over \\longrightarrow}~B,~\\) con \\( \\displaystyle~e_1=f_4-2f_3,~e_2=6f_2~~~(I_m,~I_n~\\) son matrices identidad\\()\\). Entonces:",
  opciones: [
    { texto: "\\(~\\displaystyle B=P A Q\\), on \\( \\displaystyle~I_m {\\buildrel {e_1}\\over \\longrightarrow}~P,~I_n~{\\buildrel {e_2}\\over \\longrightarrow}~Q\\).", correcta: false },
    { texto: "\\(~\\displaystyle B=P A\\), on \\( \\displaystyle~I_m {\\buildrel {e_1}\\over \\longrightarrow}~{\\buildrel {e_2}\\over \\longrightarrow}~P\\).", correcta: true },
    { texto: "\\(~\\displaystyle B=A P\\), on \\( \\displaystyle~I_n {\\buildrel {e_1}\\over \\longrightarrow}~{\\buildrel {e_2}\\over \\longrightarrow}~P\\).", correcta: false },
    { texto: "\\(~\\displaystyle B=P A Q\\), on \\( \\displaystyle~I_m {\\buildrel {e_2}\\over \\longrightarrow}~P,~I_n~{\\buildrel {e_1}\\over \\longrightarrow}~Q\\).", correcta: false }
  ]
},
{
  texto: "Sea una simetría axial en \\( \\displaystyle~\\mathbb{R}^2~~\\) respecto de una recta que pasa por \\( \\displaystyle~(0,0)~~\\) y por los cuadrantes 2 y 4, formando un ángulo de \\( \\displaystyle~{45}^{o}~~\\) con el eje de abscisas. Su matriz asociada es:",
  opciones: [
    { texto: "\\( \\displaystyle~\\left[ \\begin{array}{cc} 0&-1\\\\  -1&0 \\end{array}\\right]~\\).", correcta: true },
    { texto: "\\( \\displaystyle~\\left[ \\begin{array}{cc} \\sqrt{2}/2&\\sqrt{2}/2\\\\  -\\sqrt{2}/2&\\sqrt{2}/2\\end{array}\\right]~\\).", correcta: false },
    { texto: "\\( \\displaystyle~\\left[ \\begin{array}{cc} \\sqrt{2}/2&-\\sqrt{2}/2\\\\  -\\sqrt{2}/2&\\sqrt{2}/2\\end{array}\\right]~\\).", correcta: false }, 
    { texto: "Ninguna de las otras \\( 3 \\) respustass es correcta. ", correcta: false }
  ]
}
];

function barajar(array) {
  return array.sort(() => Math.random() - 0.5);
}

function generarCuestionario() {
  const form = document.getElementById("quizForm");
  form.innerHTML = "";

  preguntas.forEach((pregunta, index) => {
    const opcionesBarajadas = barajar([...pregunta.opciones]);

    const div = document.createElement("div");
    div.className = "question-container";

    const htmlPregunta = `
      <div class="question">
        <strong>${index + 1}. ${pregunta.texto}</strong>
      </div>
      <div class="options">
        ${opcionesBarajadas.map((op, i) => `
          <label>
            <input type="radio" name="q${index}" value="${op.correcta ? 1 : 0}">
            ${op.texto}
          </label>`).join('')}
      </div>
    `;

    div.innerHTML = htmlPregunta;
    form.appendChild(div);
  });

  MathJax.typeset();
}

function calcularResultado() {
  const form = document.getElementById('quizForm');
  const formData = new FormData(form);
  let aciertos = 0;

  preguntas.forEach((pregunta, i) => {
    const contenedor = form.querySelectorAll(".question-container")[i];
    contenedor.classList.remove("correct", "incorrect");
    const opciones = contenedor.querySelectorAll("input");

    let respondidaCorrectamente = false;

    opciones.forEach(op => {
      const label = op.closest("label");
      label.classList.remove("correct", "incorrect");

      if (op.checked) {
        if (op.value === "1") {
          label.classList.add("correct");
          respondidaCorrectamente = true;
          aciertos++;
        } else {
          label.classList.add("incorrect");
        }
      }

      // Mostrar la correcta aunque no haya sido seleccionada
      //if (op.value === "1" && !op.checked) {
        //label.classList.add("correct");
      //}
    });

    contenedor.classList.add(respondidaCorrectamente ? "correct" : "incorrect");
  });

  const porcentaje = (aciertos / preguntas.length) * 100;
  mostrarGrafico(porcentaje);
  mostrarFeedback(porcentaje);
  MathJax.typeset();
}

function mostrarGrafico(porcentaje) {
  const ctx = document.getElementById('resultChart').getContext('2d');
  if (window.myChart) window.myChart.destroy();
  window.myChart = new Chart(ctx, {
    type: 'doughnut',
    data: {
      labels: ['Aciertos', 'Errores'],
      datasets: [{
        data: [porcentaje, 100 - porcentaje],
        backgroundColor: ['#4caf50', '#f44336']
      }]
    },
    options: {
      responsive: false,
      plugins: {
        legend: {
          position: 'bottom'
        }
      }
    }
  });
}

function mostrarFeedback(p) {
  let emoji = '';
  let texto = '';
  if (p === 100) {emoji = '&#129321 &#127942'; texto = 'Excelente, todo correcto. ¡Lo has hecho genial!';}
  if (p === 80) {emoji = '&#128513  &#127941'; texto = ' Muy bien. Sólo ha faltado un poquito.';}
  if (p === 60) {emoji = '&#128515 &#128076'; texto = ' Buen trabajo. Ya casi lo tienes.';}
  if (p === 40) {emoji = '&#128522 &#128640'; texto = ' Vas tomándole el pulso. ¡Sigue así!';}
  if (p === 20) {emoji = '&#128528 &#128692'; texto = ' Por algo se empieza. ¡No te rindas!';}
  if (p === 0) {emoji = '&#128542  &#128170'; texto = ' Te recomiendo repasar los conceptos relacionados y volver a intentarlo. ¡Ánimo!';}

  document.getElementById('feedback').innerHTML =
    `
    <div style="font-size: 2em; text-align: center; margin-top: 20px;">
      <div style="font-size: 3em;">${emoji}</div>
      <p><strong>Resultado:</strong> ${p.toFixed(0)}%</p>
      <p>${texto}</p>
    </div>`;
}


function reiniciarFormulario() {
  const form = document.getElementById('quizForm');
  if (window.myChart) {
    window.myChart.destroy();
    window.myChart = null;
  }
  document.getElementById('feedback').innerHTML = '';
  generarCuestionario(); // recrea el cuestionario con orden nuevo
}

// Cargar cuestionario al inicio
generarCuestionario();
</script>

</body>
</html>